package com.seu.project.seuquiz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seu.project.seuquiz.common.ErrorCode;
import com.seu.project.seuquiz.exception.BusinessException;
import com.seu.project.seuquiz.model.dto.user.UserQueryRequest;
import com.seu.project.seuquiz.model.entity.User;
import com.seu.project.seuquiz.service.CollegeService;
import com.seu.project.seuquiz.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author SoftPto
 * @createDate: 2023-08-07 22:16
 */
@Service
public class CollegeServiceImpl implements CollegeService {

    @Resource
    private UserService userService;

    @Override
    public Integer getCollegeTotalStudent(String college) {
        if (StringUtils.isBlank(college)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "学院名称不能为空");
        }
        UserQueryRequest userQueryRequest = UserQueryRequest.builder().college(college).build();
        QueryWrapper<User> queryWrapper = userService.getQueryWrapper(userQueryRequest);
        long count = userService.count(queryWrapper);
        if (count == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "学院下无学生");
        }
        return Math.toIntExact(count);
    }
}
